<template>
  <div>
    <div class="content">
      <div>
        <MySearch :form-inline="formInline" @searchFn="searchFn">
          <template>
            <el-col :span="8">
              <el-form-item label="库区名称" prop="warehouseName">
                <el-input v-model="formInline.warehouseName" placeholder="请输入" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="库位名称" prop="name">
                <el-input v-model="formInline.name" placeholder="请输入" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="库位状态" prop="status">
                <el-select v-model="formInline.status" placeholder="请选择" clearable>
                  <el-option label="全部" :value="null" />
                  <el-option label="停用" value="0" />
                  <el-option label="启用" value="1" />
                </el-select>
              </el-form-item>
            </el-col>
          </template>
        </MySearch>
      </div>
      <div class="table">
        <div>
          <MyTable :table-data="areaList" :table-col="tableCol" :current="current" :size="size">
            <template #btn>
              <div>
                <el-button
                  class="add"
                  @click="$router.push({name:'area-details', params: {id:'null'}})"
                >新增库区</el-button>
              </div>
            </template>
            <template #edit="props">
              <el-button type="text" size="small" @click="$router.push({name:'area-details', params: {id:props.status.id}})">编辑</el-button>
              <el-button type="text" size="small" @click="editStatus(props.status)">{{ status(props.status.status) }}</el-button>
              <el-button type="text" size="small" @click="delFn(props.status)">删除</el-button>
            </template>
          </MyTable>
        </div>
        <div>
          <MyPage :total-page="totalPage" @changeSize="changeSize" @changeCurrent="changeCurrent" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { pageDetail, editArea, delArea } from '@/api'
import MySearch from '@/components/MySearch'
import MyTable from '@/components/MyTable'
import MyPage from '@/components/MyPage'
export default {
  components: { MySearch, MyTable, MyPage },
  data() {
    return {
      formInline: {},
      areaList: [],
      tableCol: [
        { prop: 'warehouseName', label: '所属仓库', width: '150px' },
        { prop: 'code', label: '库区编号', width: '150px' },
        { prop: 'name', label: '库区名称', width: '150px' },
        { prop: 'temperatureType', label: '温度类型', width: '200px' },
        { prop: 'bearingType', label: '承重类型', width: '150px' },
        { prop: 'useType', label: '用途属性', width: '150px' },
        { prop: 'textstatus', label: '库区状态', width: '150px' },
        { prop: 'personName', label: '负责人', width: '150px' },
        { prop: 'phone', label: '联系电话', width: '150px' },
        { prop: 'includedNum', label: '库区数量', width: '150px' },
        { prop: 'updateTime', label: '更新时间', width: '200px' }
      ],
      totalPage: null,
      size: 10,
      current: 1
    }
  },
  async created() {
    await this.areaFn(this.formInline, this.size, this.current)
  },
  methods: {
    async areaFn(obj, size, current) {
      const res = await pageDetail({
        ... obj,
        current,
        size
      })
      this.totalPage = Number(res.data.data.total)
      this.areaList = res.data.data.records
      // 数据处理
      this.areaList.forEach(item => {
        if (item.bearingType === 'ZX') {
          item.bearingType = '重型'
        } else if (item.bearingType === 'QX') {
          item.bearingType = '轻型'
        } else {
          item.bearingType = '中型'
        }
        if (item.temperatureType === 'CW') {
          item.temperatureType = '常温'
        } else if (item.temperatureType === 'LC') {
          item.temperatureType = '冷藏'
        } else {
          item.temperatureType = '恒温'
        }
        if (item.useType === 'RKHCQ ') {
          item.useType = '入库缓存区'
        } else if (item.useType === 'CKHCQ') {
          item.useType = '出库缓存区'
        } else if (item.useType === 'CCQ ') {
          item.useType = '存储区'
        } else if (item.useType === 'FJQ ') {
          item.useType = '分拣区'
        } else {
          item.useType = '质检区'
        }
        if (item.status === 0) {
          item.textstatus = '停用'
        } else {
          item.textstatus = '启用'
        }
      })
    },
    async searchFn() {
      const res = await this.areaFn(this.formInline, this.size, this.current)
      console.log(res)
    },
    // 改变页数
    async changeSize(val) {
      this.size = val
      await this.areaFn(this.formInline, this.size, this.current)
    },
    async changeCurrent(val) {
      this.current = val
      await this.areaFn(this.formInline, this.size, this.current)
    },
    // 修改状态
    editStatus(data) {
      this.$confirm(`确定要库区${this.status(data.status)}：${data.name} 吗？`, `确认${this.status(data.status)}`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await editArea({ id: data.id, status: data.status === 1 ? 0 : 1 })
        await this.areaFn(this.formInline, this.size, this.current)
        this.$message({
          type: 'success',
          message: `${this.status(data.status)}成功!`
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: `已取消${this.status(data.status)}`
        })
      })
    },
    async delFn(data) {
      this.$confirm(`确定要删除吗？`, `提示`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delArea(data.id)
        await this.areaFn(this.formInline, this.size, this.current)
        this.$message({
          type: 'success',
          message: `删除成功!`
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: `已取消删除`
        })
      })
    },
    // 状态显示
    status(status) {
      return status === 1 ? '停用' : '启用'
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/my-form.scss';
</style>
